﻿using BMS.Visitor.Model.Entities;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace BMS.Visitor.Dao
{
    public class DaoVisitorHelper : IDaoHelper<BMS.Visitor.Model.Entities.Visitor>
    {
        private const string CONNECTION_STRING = @"Integrated Security=True;Persist Security Info=False;Initial Catalog=TimeWork;Data Source=LOCALHOST\MSSQLSERVER_2;Trusted_Connection=True;";
        private SqlConnection connection;
        private SqlTransaction transaction;
        private SqlCommand command;

        public DaoVisitorHelper()
        {
            connection = new SqlConnection(CONNECTION_STRING);
            command = new SqlCommand();
        }

        public void Save(BMS.Visitor.Model.Entities.Visitor visitor)
        {
            try
            {
                connection.Open();

                transaction = connection.BeginTransaction();

                command.Transaction = transaction;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "[TWA].[CreateVisitor]";
                command.Connection = connection;

                command.Parameters.Add("@DOCUMENT_ID", System.Data.SqlDbType.VarChar).Value = visitor.DocumentId;
                command.Parameters.Add("@NAME", System.Data.SqlDbType.VarChar).Value = visitor.Name;
                command.Parameters.Add("@LASTNAME", System.Data.SqlDbType.VarChar).Value = visitor.LastName;
                command.Parameters.Add("@GENERE", System.Data.SqlDbType.Char).Value = visitor.Genere;
                command.Parameters.Add("@ACTIVE", System.Data.SqlDbType.Bit).Value = visitor.Active;
                command.Parameters.Add("@PHOTO_PATH", System.Data.SqlDbType.VarChar).Value = visitor.PhotoPath;
                command.Parameters.Add("@FINGER_PRINT", System.Data.SqlDbType.VarBinary).Value = visitor.FingerPrint;

                command.ExecuteNonQuery();

                transaction.Commit();
            }
            catch(Exception exc)
            {
                transaction.Rollback();
            }
            finally
            {
                connection.Close();
            }
        }

        public void Update(BMS.Visitor.Model.Entities.Visitor persistenceObject)
        {
            throw new NotImplementedException();
        }

        public void Delete(BMS.Visitor.Model.Entities.Visitor persistenceObject)
        {
            throw new NotImplementedException();
        }

        public BMS.Visitor.Model.Entities.Visitor Get(string propertyName, string value)
        {
            throw new NotImplementedException();
        }

        public List<BMS.Visitor.Model.Entities.Visitor> List()
        {
            throw new NotImplementedException();
        }
    }
}
